﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace GenerateXMLfile
{
    class CorpusGenerator
    {
        static void Main(string[] args)
        {

            string connectionString = @"Data Source= (local); Initial Catalog=KBBI;Integrated Security=SSPI;";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            string Lines = string.Empty;

            TextWriter tw = new StreamWriter(@"C:\Users\Laras Mutiara Diva\Desktop\Laras code.google\docs\stemmed-corpus1000.xml");

            TextReader rd = new StreamReader(@"C:\Users\Laras Mutiara Diva\Desktop\Laras code.google\docs\corpus1000-pertanian.xml");
            var line = string.Empty;
            int c = 0;
            int n = 1;
            //SqlCommand command = new SqlCommand();
            while ((line = rd.ReadLine()) != null) 
            {
                if (n > 8)  
                {
                    var wordLine = getWords(line);
                    foreach (var word in wordLine)
                    {

                        SqlCommand command = new SqlCommand("SELECT dbo.isStopWord(@word) as stemmedWord", connection);
                        command.Parameters.Add(new SqlParameter("@word", word.ToLower()));
                        SqlDataReader reader = command.ExecuteReader();
                        reader.Read();
                        if (!(reader["stemmedWord"].Equals("0")))       //if stem result == string "0" then don't merge to line
                        {
                            Lines += reader["stemmedWord"] + " ";

                        }
                        reader.Close();
                        //command.


                    }
                }
                else
                {
                    Lines += line;      //for top 8 rows
                }
                Lines += "\n";
                Console.WriteLine(c++);
                n++;
                //tw.WriteLine(line);

            }
            tw.Write(Lines);        //write lines to new file
            tw.Close();

            connection.Close();
             Console.ReadKey();
            

        }

        //static List<string> GetWords(string isiFile) // mengambil kata per kata(token) yang ada pada string yang panjang dan diletakkan di ebuah list
        //{
        //    var temp = Regex.Split(isiFile, @"\w+"); // menghilangkan spasi dan tanda baca lainnya dan mengembalikan kata2
        //    var result = from i in temp             //LINQ for checking if word is not empty string
        //                 where i.Length > 0
        //                 select i;                  //get words that aren't empty

        //    return result.ToList();
        //}

        static List<string> getWords(string lineString)
        { 
            var delimiter = new List<char>{
                                            ' ', '`', '~', '!', '#', 
                                            '$', '@' , '%', '^', '&', 
                                            '*', '(', ')', '-', '_', 
                                            '+', '=', '{', '}', '[', ']', 
                                            '\\', ':', ';', '"', '\'', 
                                            ',', '.', '?', '/' 
            };


            List<string> words = new List<string>();
            var tempString = string.Empty;
            int i = 0;
            
            for ( i=0; i < lineString.Length; i++)
            {
                if (i > 0)
                {
                    if (                                        //in tokenization, all xml tag aren't removed
                        delimiter.Contains(lineString[i])
                        &&
                        !(lineString[i - 1] == '<' && lineString[i] == '/')
                        &&
                        !(lineString[i - 1] == 'x' && lineString[i] == ':')
                        &&
                        !(lineString[i] == '=' && lineString.TakeBefore(i, 2) == "id")
                        &&
                        !(lineString[i] == '"' && (lineString.TakeBefore(i, 3) == "id=" || lineString.TakeAfter(i - 1, 2) == "\">"))
                        
                        &&
                        !(lineString[i] == '.' && (
                                                    //!lineString.TakeBefore(i, 2).isNumber()
                                                   // &&
                                                    (
                                                        lineString.TakeAfter(i,1)==" "
                                                        ||
                                                        lineString.TakeAfter(i, 1) == "\n"
                                                        
                                                    )
                                                    &&
                                                    !lineString.TakeBefore(i,2).ToLower().Equals("rp")
                                                  )
                         )
                         
                        
                        
                       )
                    {
                        if (tempString.Length > 0)
                            words.Add(tempString);

                        tempString = string.Empty;
                    }
                    else
                    {
                       
                        tempString += lineString[i];
                    }
                }
                else
                {
                    if(!delimiter.Contains(lineString[i]))
                        tempString += lineString[i];
                }
            }
            //Console.WriteLine(tempString);
            if(i==lineString.Length)
                words.Add(tempString);

            return words;
        }

       
    }

    static class MyClass
    {
        public static string TakeBefore(this string input, int current, int n)
        {
            if (current < n)
                n = current;
           
                string temp = string.Empty;

                while (n != 0)
                {
                    temp += input[current - n];
                    n--;
                }
           
            return temp;
        }
        public static string TakeAfter(this string input, int current, int n)
        {
            if (input.Length-1 == current)
            {
                return "\n";
            }

            if (current+n > input.Length-1)
                 n = input.Length-current-1;

            string temp = string.Empty;
            int i = current+1;
            while (i <= current + n)
            {
                temp += input[i];
                i++;
            }

            return temp;
        }

        public static bool isNumber(this string inputString)
        {
            bool number = true;
            if (inputString == string.Empty)
                number = false;
            /*
            foreach (var myChar in inputString)
            {
                if(number)
                    number = Char.IsNumber(myChar);
            }
            */

            for (int i = 0; i < inputString.Length && number; i++)
            {
                number = Char.IsNumber(inputString[i]);
                //Console.WriteLine(i);
            }
            return number;
        }
    }
}
